package com.atguigu.tingshu.album.api;

import com.atguigu.tingshu.album.service.TrackInfoService;
import com.atguigu.tingshu.album.service.VodService;
import com.atguigu.tingshu.common.execption.GuiguException;
import com.atguigu.tingshu.common.login.GuiguLogin;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.common.result.ResultCodeEnum;
import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.model.album.TrackInfo;
import com.atguigu.tingshu.model.user.UserInfo;
import com.atguigu.tingshu.model.user.UserListenProcess;
import com.atguigu.tingshu.query.album.TrackInfoQuery;
import com.atguigu.tingshu.vo.album.AlbumTrackListVo;
import com.atguigu.tingshu.vo.album.TrackInfoVo;
import com.atguigu.tingshu.vo.album.TrackListVo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import org.apache.catalina.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;

@Tag(name = "声音管理")
@RestController
@RequestMapping("api/album/trackInfo")
@SuppressWarnings({"all"})
public class TrackInfoApiController {

	@Autowired
	private TrackInfoService trackInfoService;

	@Autowired
	private VodService vodService;

	@Autowired
	private RedisTemplate redisTemplate;

	@Operation(summary = "批量获取下单付费声音列表")
	@GetMapping("findPaidTrackInfoList/{trackId}/{trackCount}")
	public Result<List<TrackInfo>>
	                 findPaidTrackInfoList(@PathVariable Long trackId,
											@PathVariable Integer trackCount) {
		List<TrackInfo> list =
				trackInfoService.findPaidTrackInfoList(trackId,trackCount);
		return Result.ok(list);
	}

	@GuiguLogin(required = false)
	@Operation(summary = "获取专辑声音分页列表")
	@GetMapping("findAlbumTrackPage/{albumId}/{page}/{limit}")
	public Result<IPage<AlbumTrackListVo>> findAlbumTrackPage(
			@PathVariable Long albumId,
			@PathVariable Long page,
			@PathVariable Long limit) {
		//用户id
		Long userId = AuthContextHolder.getUserId();
		Page<AlbumTrackListVo> pageParam = new Page(page,limit);
		IPage<AlbumTrackListVo> pageModel = trackInfoService.findAlbumTrackPage(pageParam,albumId,userId);
		return Result.ok(pageModel);
	}

	//修改声音
	@PutMapping("updateTrackInfo/{trackId}")
	public Result updateTrackInfo(@PathVariable("trackId") Long trackId,
								  @RequestBody @Validated TrackInfoVo trackInfoVo) {
		trackInfoService.updateTrackInfo(trackId,trackInfoVo);
		return Result.ok();
	}

	//修改-根据声音id查询
	@GetMapping("getTrackInfo/{trackId}")
	public Result getTrackInfo(@PathVariable("trackId") Long trackId) {
		TrackInfo trackInfo = trackInfoService.getById(trackId);
		return Result.ok(trackInfo);
	}

	//删除声音
	@DeleteMapping("removeTrackInfo/{id}")
	public Result removeTrackInfo(@PathVariable Long id) {
		trackInfoService.removeTrackInfo(id);
		return Result.ok();
	}

	//声音列表
	@GuiguLogin(required = true)
	@Operation(summary = "获取当前用户声音分页列表")
	@PostMapping("findUserTrackPage/{page}/{limit}")
	public Result<IPage<TrackListVo>>
					findUserTrackPage(@PathVariable Long page,
									@PathVariable Long limit,
									@RequestBody TrackInfoQuery trackInfoQuery,
									  HttpServletRequest request){
//* **1、从请求头获取token（前端传递过来的）**
//		String token = request.getHeader("token");
//		if(StringUtils.isEmpty(token)) {//不是登录
//			throw new GuiguException(ResultCodeEnum.LOGIN_AUTH);
//		}

//* **2、根据token查询redis（redis的key是token），如果可以查询到是登录，查询不到不是登录**
//		UserInfo userInfo = (UserInfo)redisTemplate.opsForValue().get(token);
//		if(userInfo == null) {//不是登录
//			throw new GuiguException(ResultCodeEnum.LOGIN_AUTH);
//		}

		trackInfoQuery.setUserId(1L);
		Page<TrackListVo> pageParam = new Page<>(page, limit);
		IPage<TrackListVo> trackListVoIPage =
				trackInfoService.findUserTrackPage(pageParam,trackInfoQuery);
		//	返回数据
		return Result.ok(trackListVoIPage);
	}

	/**
	 * 保存声音
	 * @param trackInfoVo
	 * @return
	 */
	@Operation(summary = "新增声音")
	@PostMapping("saveTrackInfo")
	public Result saveTrackInfo(@RequestBody @Validated TrackInfoVo trackInfoVo) {
		trackInfoService.saveTrackInfo(trackInfoVo);
		return Result.ok();
	}


	/**
	 * 上传声音
	 * @param file
	 * @return
	 */
	@Operation(summary = "上传声音")
	@PostMapping("uploadTrack")
	public Result<Map<String,Object>> uploadTrack(MultipartFile file) {

		//	调用服务层方法
		// 上传之后，腾讯云返回两个值，文件在腾讯云id 和 生成音频文件播放地址
		Map<String,Object> map = vodService.uploadTrack(file);
		return Result.ok(map);
	}

	//显示可以购买声音集数
	@GuiguLogin
	@Operation(summary = "获取用户声音分集购买支付列表")
	@GetMapping("/findUserTrackPaidList/{trackId}")
	public Result<List<Map<String, Object>>> findUserTrackPaidList(@PathVariable Long trackId) {
		List<Map<String, Object>> list =
				trackInfoService.findUserTrackPaidList(trackId);
		return Result.ok(list);
	}
}

